﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConvertToGif
{
    /// <summary>
    /// Gif图片格式转换帮助类
    /// </summary>
    public class GifConvert
    {
        /// <summary>
        /// 当前转换文件信息
        /// </summary>
        public GifConverData ConverData { get; set; }
        /// <summary>
        /// 处理工具地址
        /// </summary>
        public string FFmpegTool { get; set; }
        /// <summary>
        /// 当前处理百分比，保留字段
        /// </summary>
        public double CurrentPercent { get; set; }

        /// <summary>
        /// 相应结果时，处理
        /// </summary>
        public Action<string> OnResponse { get; set; }
        /// <summary>
        /// 转换结束事件处理
        /// </summary>
        public Action<string> OnEnd { get; set; }
        /// <summary>
        /// 执行开始时间
        /// </summary>
        private DateTime StartTime { get; set; }
        /// <summary>
        /// 执行结束事件
        /// </summary>
        private DateTime EndTime { get; set; }

        /// <summary>
        /// 初始化处理
        /// </summary>
        /// <param name="data"></param>
        public GifConvert(GifConverData data, string FFmpegTool = null)
        {
            this.ConverData = data;
            //初始化处理,如果不指定工具位置，则自动当前程序目录的ffmpeg.exe文件
            if (string.IsNullOrEmpty(FFmpegTool))
            {
                this.FFmpegTool = Common.LocalPathHelper.CurrentProgram + "\\ffmpeg.exe";
            }
            else
            {
                this.FFmpegTool = FFmpegTool;
            }
            if (File.Exists(this.FFmpegTool) == false)
                throw new Exception($"ffmpeg.exe处理程序没有找到，{this.FFmpegTool}");
        }
        /// <summary>
        /// 开始转换
        /// </summary>
        public void Start()
        {
            Process p = new Process();
            p.StartInfo.FileName = this.FFmpegTool;
            p.StartInfo.Arguments = ConverData.GetArguments();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.ErrorDataReceived += new DataReceivedEventHandler(Output);
            p.Start();//启动
            p.BeginErrorReadLine();//开始异步读取
            p.WaitForExit();//阻塞进程结束
            p.Close();
            p.Dispose();

            //触发结束
            this.EndTime = DateTime.Now;
            if (this.OnEnd != null)
            {
                TimeSpan span = this.EndTime - this.StartTime;
                this.OnEnd(span.Hours + "小时，" + span.Minutes + "分，" + span.Seconds + "秒");
            }
        }
        /// <summary>
        /// 输出内容处理
        /// </summary>
        private void Output(object sender, DataReceivedEventArgs e)
        {
            string data = e.Data;
            //开始标记
            if (StartTime == DateTime.MinValue)
                StartTime = DateTime.Now;
            if (!string.IsNullOrEmpty(data))
            {
                //执行委托处理
                if (OnResponse != null)
                {
                    //响应触发
                    OnResponse(data);
                }
            }
        }
    }
}
